package org.jash.launcher.model

import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import org.jash.common.mvi.BaseViewModel
import org.jash.common.R
import org.jash.common.mvi.BaseState
import org.jash.common.net.retrofit
import org.jash.launcher.entity.GatewayEntity
import org.jash.launcher.net.LauncherService

class MessageViewModel:BaseViewModel<MessageIntent, MessageState>() {
    private val service by lazy { retrofit.create(LauncherService::class.java) }
    fun createGateway(getMessage: MessageIntent.GetMessage) {
        viewModelScope.launch {
            state.value = try {
                val res = service.getMessage(mapOf("userId" to getMessage.userId, "page" to 1))
                if (res.statusCode == 200) {
                    MessageState.Response(res.data)
                } else {
                    BaseState.Error(res.msg)
                }
            } catch (e:Exception) {
                BaseState.Error(e.message ?: "")
            }
        }
    }
}